package com.example.viewtest.DialogGroup;

import android.app.AlertDialog;
import android.app.Dialog;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.Gravity;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

import androidx.annotation.NonNull;

import com.example.viewtest.ActivityGroup.DiaLogActivity;
import com.example.viewtest.Dao.MessageEvent;
import com.example.viewtest.R;

import org.greenrobot.eventbus.EventBus;

//自定义对话框
public class diaLogTest extends Dialog {

    private Button yes;
    private Button no;
    private Button goTo;
    //确认、取消的文本内容
    private String yesStr;
    private String noStr;

    private Context mContext;
    private View view;
    private String starttime_text;
    private String endtime_text;
    private String contain_text;
    private TextView starttime;
    private TextView endtime;
    private TextView containtext;
    //
    private EditText editText;

    //取消按钮被点击了的监听器
    private onNoOnclickListener noOnclickListener;
    //确定按钮被点击了的监听器
    private onYesOnclickListener yesOnclickListener;

    private onGoToAnotherActivityOnclickListener goToAnotherActivityOnclickListener;

    public diaLogTest(@NonNull Context context) {
        super(context);
    }

    public diaLogTest(@NonNull Context context,String starttime_text,String endtime_text,String contain_text){
        super(context, R.style.BottomDialog);
        mContext = context;
        this.starttime_text=starttime_text;
        this.endtime_text=endtime_text;
        this.contain_text=contain_text;
    }
    /**
     * 设置取消按钮的显示内容和监听
     *
     * @param str
     * @param onNoOnclickListener
     */
    public void setNoOnclickListener(String str, onNoOnclickListener onNoOnclickListener) {
        if (str != null) {
            noStr = str;
        }
        this.noOnclickListener = onNoOnclickListener;
    }

    /**
     * 设置确定按钮的显示内容和监听
     *
     * @param str
     * @param onYesOnclickListener
     */
    public void setYesOnclickListener(String str, onYesOnclickListener onYesOnclickListener) {
        if (str != null) {
            yesStr = str;
        }
        this.yesOnclickListener = onYesOnclickListener;
    }

    public void setGoToAnotherActivityOnclickListener(onGoToAnotherActivityOnclickListener onGoTo){
        this.goToAnotherActivityOnclickListener = onGoTo;
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        //绑定对话框布局
        setContentView(R.layout.test_dialog);
        //对话框外界触摸是否取消对话框
        setCanceledOnTouchOutside(false);
        //点击返回键是否取消对话框
        setCancelable(false);

        setTitle("对话框");

        Window window = getWindow();
        window.setWindowAnimations(R.style.mystyle);
        WindowManager.LayoutParams windowAttributes = window.getAttributes();
        windowAttributes.gravity = Gravity.BOTTOM;
        windowAttributes.width = WindowManager.LayoutParams.MATCH_PARENT;
        windowAttributes.height = 1200;
        window.setAttributes(windowAttributes);

        initview();
        //初始化界面控件的事件
        initEvent();
    }
    private void initview() {
        starttime=(TextView)findViewById(R.id.starttime_detail);
        endtime=(TextView)findViewById(R.id.endtime_detail);
        containtext=(TextView)findViewById(R.id.containtext_detail);
        editText = findViewById(R.id.edit_dialog);

        yes = findViewById(R.id.yes_dialog);
        no = findViewById(R.id.no_dialog);
        goTo = findViewById(R.id.gotoActivity_dialog);

        starttime.setText(starttime_text);
        endtime.setText(endtime_text);
        containtext.setText(contain_text);
    }

    /**
     * 初始化界面的确定和取消监听器
     */
    private void initEvent() {
        //设置确定按钮被点击后，向外界提供监听
        yes.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //发布事件
                EventBus.getDefault().post(new MessageEvent(editText.getText().toString()));
                if (yesOnclickListener != null) {
                    yesOnclickListener.onYesClick();
                }
            }
        });
        //设置取消按钮被点击后，向外界提供监听
        no.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (noOnclickListener != null) {
                    noOnclickListener.onNoClick();
                }
            }
        });

        goTo.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (goToAnotherActivityOnclickListener != null){
                    goToAnotherActivityOnclickListener.onGoToClick();
                    //跳转主Activity
                    Intent intent = new Intent("android.intent.action.DEFINE");
                    intent.addCategory("android.intent.category.DEFAULT");
                    mContext.startActivity(intent);
                }
            }
        });

    }



    public interface onYesOnclickListener {
        public void onYesClick();
    }

    public interface onNoOnclickListener {
        public void onNoClick();
    }

    public interface onGoToAnotherActivityOnclickListener{
        void onGoToClick();
    }

}
